package cn.tedu.luban.worker.main.converter;

import cn.tedu.luban.passport.protocol.jwt.UserPrinciple;
import cn.tedu.luban.passport.sdk.security.LubanSecurityContext;
import cn.tedu.luban.worker.protocol.VO.AdminAuditDetailVO;
import cn.tedu.luban.worker.protocol.VO.AdminAuditInfoVo;
import cn.tedu.luban.worker.protocol.VO.AdminAuditWorkerInfoVO;
import cn.tedu.luban.worker.protocol.bo.AdminAuditBO;
import cn.tedu.luban.worker.protocol.bo.AdminWorkerBO;
import cn.tedu.luban.worker.protocol.dos.WorkerAuditLogDO;
import cn.tedu.luban.worker.protocol.param.AuditSubmitParam;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

@Component
public class WorkerAuditConverter {
    public AdminAuditDetailVO bo2vo(AdminWorkerBO bo) {
        AdminAuditDetailVO adminAuditDetailVO = new AdminAuditDetailVO();
        //审核内容
        adminAuditDetailVO.setAuditInfo(this.bos2vos(bo.getWorkerAuditLogs()));
        //审核意见
        adminAuditDetailVO.setAuditWorkerInfoVO(bo2AuditVo(bo));
        return adminAuditDetailVO;
    }
    public AdminAuditBO do2bo(WorkerAuditLogDO workerAuditLogDO) {
        AdminAuditBO adminAuditBO =new AdminAuditBO();
        if (workerAuditLogDO!=null){
            BeanUtils.copyProperties(workerAuditLogDO, adminAuditBO);
        }
        return adminAuditBO;
    }
    public List<AdminAuditBO> dos2bos(List<WorkerAuditLogDO> dos) {
        List<AdminAuditBO> result = new ArrayList<>();
        if (CollectionUtils.isEmpty(dos)) {
            return result;
        }
        for (WorkerAuditLogDO workerAuditLogDO : dos) {
            result.add(do2bo(workerAuditLogDO));
        }
        return result;
    }

    public WorkerAuditLogDO param2do(AuditSubmitParam param) {
        WorkerAuditLogDO audit = new WorkerAuditLogDO();
        BeanUtils.copyProperties(param, audit);
        //登录用户 审核操作的登录用户,管理员
        UserPrinciple loginUser = LubanSecurityContext.getLoginToken();
        audit.setOperateTime(System.currentTimeMillis());
        audit.setGmtCreate(System.currentTimeMillis());
        audit.setGmtModified(audit.getGmtCreate());
        audit.setCreateUserId(loginUser.getId());
        audit.setModifiedUserId(loginUser.getId());
        audit.setCreateUserName(loginUser.getUsername());
        audit.setModifiedUserName(loginUser.getUsername());
        return audit;
    }
    private List<AdminAuditInfoVo> bos2vos(List<AdminAuditBO> logs) {
        if (CollectionUtils.isEmpty(logs)) {
            return Collections.emptyList();
        }
        List<AdminAuditInfoVo> result = new ArrayList<>();
        logs.forEach(bo->{
            AdminAuditInfoVo adminAuditInfoVo = new AdminAuditInfoVo();
            BeanUtils.copyProperties(bo, adminAuditInfoVo);
            result.add(adminAuditInfoVo);
        });
        return result;
    }
    private AdminAuditWorkerInfoVO bo2AuditVo(AdminWorkerBO bo) {
        AdminAuditWorkerInfoVO adminAuditWorkerInfoVO = new AdminAuditWorkerInfoVO();
        BeanUtils.copyProperties(bo, adminAuditWorkerInfoVO);
        adminAuditWorkerInfoVO.setWorkerId(bo.getUserId());
        return adminAuditWorkerInfoVO;
    }
}
